﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection conn;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ShopLaptopConnectionString"].ConnectionString);
        if (!IsPostBack)
        {
            //DisplayDrdlProductSearch();
            //displayMenu();
            DisplayProduct();
        }

    }
    
    protected void DisplayProduct()
    {
        string cateID = Request.QueryString["cateID"];
        string proName = Request.QueryString["proName"];
        SqlCommand cmd = new SqlCommand();
        cmd.Connection=conn;
        if (cateID==null && proName == null)
        {
            cmd.CommandText = "Select * from Product where prostatus=1";
        }
        else if(proName==null && cateID!=null)
        {
            cmd.CommandText = "Select * from Product where cateid="+cateID+" and prostatus=1";
        }
        else if (proName!=null && cateID==null)
        {
            cmd.CommandText = "Select * from Product where ProName like '%"+proName+"%' and prostatus=1";
        }
        else if (proName != null && cateID != null)
        {
            cmd.CommandText = "Select * from Product where ProName like '%" + proName + "%' and cateID="+cateID+" and prostatus=1";
        }
        conn.Open();
        //DataSet ds = new DataSet();
        //da.Fill(ds);
        //CollectionPager1.PageSize = 6;
        //CollectionPager1.DataSource = ds.Tables[0].DefaultView;
        //CollectionPager1.BindToControl = dlProduct;
        SqlDataReader reader = cmd.ExecuteReader();
        dlProduct.DataSource = reader;
        dlProduct.DataBind();
        conn.Close();
    }
    
    protected void dlProduct_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        Label lbproID = e.Item.FindControl("label3") as Label;
        Response.Redirect("ProductDetail.aspx?proID=" + lbproID.Text);
    }

    protected void dlProduct_EditCommand(object source, DataListCommandEventArgs e)
    {
        int proID = Convert.ToInt32(((Label)e.Item.FindControl("Label3")).Text);
        
        ShopCart ca = new ShopCart();
        ca.ProID = proID;
        
        ArrayList listProID = new ArrayList();
        if (Session["proID"] == null)
        {
            ca.SoLuong = 1;
            listProID.Add(ca);
        }
        else
        {
            listProID.AddRange((ArrayList)Session["proID"]);
            //bool check = true;
            foreach (object a in listProID)
            {
                ShopCart newCa = (ShopCart)a;
                if (newCa.ProID == proID)
                {
                    //check = false;
                    newCa.SoLuong++;
                    return;
                }
            }
            //if (check)
            //{
                ca.SoLuong = 1;
                listProID.Add(ca);
            //}
        }
        Session["proID"] = listProID;
        Response.Redirect("Cart.aspx");
    }


    protected void lbtnShopCart_Click(object sender, EventArgs e)
    {
        if (Session["proID"] != null)
        {
            Response.Redirect("Cart.aspx");
        }
        Response.Redirect("CartEmpty.aspx");
    }
}
